<?php
namespace app\admin\controller;

use think\Request;
use app\common\controller\Upload;
use app\common\model\Bloger;
use think\Exception;

class Blog extends Base
{
	public function index(Request $request)
	{
		if ($request -> isAjax()) {
			$data = $request->param('');
			try {
				$data['birth'] = strtotime($data['birth']);
				$data['year'] = date('Y',$data['birth']);
				$data['month'] = date('m',$data['birth']);
				$data['day'] = date('d',$data['birth']);
				if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$data['headurl'],$result)) {
					$data['headurl'] = Upload::base64($data['headurl'], config('path.headurl'));
				}
				unset($data['birth']);
				if(array_key_exists('id', $data)){
					Bloger::get($data['id'])->save($data);
				} else {
					Bloger::create($data);
				}
				
			} catch (Exception $e) {
				return $this->error($e->getMessage());
			}
			return $this->success('设置成功');
		}
		$info = Bloger::get(1)->toArray();
		$info['birth'] = $info['year'].'-'.$info['month'].'-'.$info['day'];
		$this->assign('info',json_encode($info));
		return view();
	}
}